package com.example.androidmqtt.Controller;

import com.example.androidmqtt.Entry.Message;
import com.example.androidmqtt.Service.ServiceImpl.PlateService;
import com.example.androidmqtt.Service.ServiceImpl.UserService;
import com.example.androidmqtt.Vo.ResultData;
import com.example.androidmqtt.Entry.User;
import com.example.androidmqtt.Vo.UserMessage;
import com.example.androidmqtt.Vo.UserMore;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author: pjg
 * @time: 2022/9/13
 */

@CrossOrigin
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private PlateService plateService;


    //使用本地绝对地址，使用项目地址有不同步问题，正在运行的项目无法找到上传后的文件
    //头像保存路径
    private static final String FILE_PATH = "D:\\File\\Android\\AndroidMqtt\\src\\main\\resources\\static\\avatar\\";


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultData UserLogin(User user) {
        UserMore login = userService.Login(user.getUserName(), user.getUserPassword());
        //登陆成功返回用户对象
        return login != null ? ResultData.success("登陆成功~", 1, login) :
                ResultData.error("用户名或者密码错误~");
    }

    //订阅模块
    @RequestMapping(value = "/subscribe", method = RequestMethod.POST)
    public ResultData UserSubscribe(int userId, Integer subId) {
        int subscribe = plateService.Subscribe(userId, subId);

        return subscribe > 0 ? ResultData.success("订阅成功", subscribe, null) :
                subscribe == -1 ? ResultData.error("不可重复订阅") : ResultData.error("订阅失败");
    }

    //取消订阅模块
    @RequestMapping(value = "/unsubscribe", method = RequestMethod.POST)
    public ResultData UserUnSubscribe(int userId, Integer subId) {
        int subscribe = plateService.unSubscribe(userId, subId);
        return subscribe > 0 ? ResultData.success("取消订阅成功", subscribe, null) :
                ResultData.error("取消订阅失败");
    }


    //修改个人信息
    @RequestMapping(value = "/changeInfo", method = RequestMethod.POST)
    public ResultData UserChangeInfo(@RequestParam("userId") Integer userId,
                                     @RequestParam(value = "userPassword", required = false) String userPassword,
                                     @RequestParam(value = "userAvatar", required = false) String userAvatar) {
//        String file = "";
//        String filename = "";
//        try
//            if (!userAvatar.isEmpty()) {
//                //文件名
//                file = userAvatar.getOriginalFilename();
//                //文件名称
//                String name = file.substring(0, file.lastIndexOf("."));
//                //文件后缀格式
//                String suffix = file.substring(file.lastIndexOf("."));
//
//                filename = FILE_PATH + System.currentTimeMillis() + name + suffix;
//                //文件保存
//                userAvatar.transferTo(new File(filename));
//            }
            /*
            保存地址到数据库
            TODO
            */
        if (Objects.equals(userPassword, ""))
            userPassword = null;
        if (Objects.equals(userAvatar, ""))
            userAvatar = null;
        User one = new User();
        one.setUserId(userId);

        one.setUserPassword(userPassword);
        one.setUserAvatar(userAvatar);
        int i = userService.ChangeInfo(one);
        return i > 0 ? ResultData.success("修改成功", i, null) :
                ResultData.error("修改失败");
    }


    //用户消息阅读
    @RequestMapping(value = "/getMessage",method = RequestMethod.GET)
    public ResultData GetMessage(Integer userId){
        List<UserMessage> userMessage = userService.getUserMessage(userId);
        return userMessage==null? ResultData.error("没有消息"): ResultData.success("获取成功",userMessage.size(),userMessage);
    }

    //用户消息已读
    @RequestMapping(value = "/hasRead",method = RequestMethod.POST)
    public ResultData MessageHasRead(Integer userId, Integer newsId){
        int i = userService.setMessageRead(userId, newsId);
        return i>0? ResultData.success(): ResultData.error("修改失败") ;
    }


}
